import { expect } from '@jest/globals';
import execa from 'execa';
import fs from 'fs';
import path from 'path';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel, IsEnable,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const testCaseDescription = `
1、新建api8+工程
2、打开cmd，切换到工程路径下
3、执行命令：./hvigorw -h
4、执行命令：./hvigorw assembleHap，等待执行完成
5、检查工程/.hvigor/report目录
`;

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  }
};

const testCaseFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  const helpOutput = await runHvigorAsync(projectRootPath, ['-h']);
  expect(helpOutput.stdout).toContain('--no-analyze');

  const result = await runHvigorAsync(projectRootPath, initExecuteCommand({
    tasks: 'assembleHap',
  }));
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  const reportDirFiles = fs.readdirSync(path.resolve(projectRootPath, './.hvigor/report'));
  const isReportJsonExist = reportDirFiles.every(fileName =>
    fileName.startsWith('report-') && fileName.endsWith('.json')
  );
  expect(isReportJsonExist).toBe(true);
};


TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_DevEco_Studio_Build_Visualization_Of_Build_07')
  .description(testCaseDescription)
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testCaseFun)
  .load();